import Vue from "vue";
import {CURRENT_PLAYING_MUSIC} from "@/store/mutation-types";
import {songUrl} from "@/api/api";
import store from '@/store'

const musicUtil = {
    handleMusicInfo(music, url) {
        return {
            'id': music.id,
            'name': music.name,
            'url': url,
            'author': music.author,
            'coverImgUrl': music.coverImgUrl,
            'duration': music.duration,
        }
    },
    play(bg, url, musicList) {
        bg.musicPlayer.musicList = musicList;
        bg.musicPlayer.context.src = url;
        bg.musicPlayer.context.play();
    },
}
export default musicUtil


/**
 * 判断播放模式
 */
export function modePlay(musicPlayer) {
    switch (musicPlayer.playMode) {
        case 'simple': // 单曲播放
            simplePlay(musicPlayer);
            break;
        case 'listLoop': // 列表循环
            loopPlay(musicPlayer);
            break;
        case 'order': // 顺序播放
            orderPlay(musicPlayer);
            break;
        default:
            loopPlay(musicPlayer);
            break;
    }
}

// 单曲播放
function simplePlay(musicPlayer) {
    let index = musicPlayer.musicList.indexOf(musicPlayer.currentMusic);
    readyNext(index, musicPlayer);
}

// 列表循环
function loopPlay(musicPlayer) {
    let playingListLength = musicPlayer.musicList;
    let currentIndex = musicPlayer.musicList.indexOf(musicPlayer.currentMusic)
    if (currentIndex === playingListLength.length - 1) {
        readyNext(0, musicPlayer)
    } else {
        readyNext(currentIndex + 1, musicPlayer)
    }
}

// 顺序播放
function orderPlay(musicPlayer) {
    let playingListLength = musicPlayer.musicList.length;
    let currentIndex = musicPlayer.musicList.indexOf(musicPlayer.currentMusic);
    if (currentIndex !== playingListLength.length - 1) {
        if (currentIndex < 0) {
            readyNext(0, musicPlayer)
        } else {
            readyNext(currentIndex + 1, musicPlayer);
        }
    }
}

// 准备播放
export function readyNext(index, musicPlayer, clickFlag) {
    musicPlayer.currentMusic = musicPlayer.musicList[index];
    Vue.ls.set(CURRENT_PLAYING_MUSIC, musicPlayer.currentMusic);
    play(musicPlayer, 1);
}

// 播放
export function play(musicPlayer, clickFlag) {
    let params = {
        'id': musicPlayer.currentMusic.id
    }
    // 在播放的时候在去获取音乐url
    songUrl(params).then((res) => {
        musicPlayer.currentMusic.url = res.data[0].url
        musicPlayer.context.src = res.data[0].url
        let currentMusic = musicPlayer.currentMusic;
        currentMusic.url = res.data[0].url;
        store.commit('SET_CURRENTMUSIC', currentMusic)
        // 更新封面和控制器
        let eventBus = Vue.prototype.$eventBus;
        musicPlayer.playState = true;
        eventBus.$emit('updateControl', currentMusic)
        eventBus.$emit('updateCover')
        if (clickFlag) { // 点击选择或自动播放中
            startPlay(musicPlayer)
        } else { // 点击播放按钮
            toggle(musicPlayer);
        }
    })
}

function startPlay(musicPlayer) {
    musicPlayer.context.play();
}

/**
 * 播放或暂停
 */
export function toggle(musicPlayer) {
    if (!musicPlayer.musicList || musicPlayer.musicList.length === 0) {
        var currentMusic = eval("(" + localStorage.getItem("current_playing_music") + ")");
        if (currentMusic) {
            musicPlayer.currentMusic = currentMusic.value;
        }
        if (!musicPlayer.musicList) {
            musicPlayer.musicList = []
        }
        musicPlayer.musicList.push(musicPlayer.currentMusic)
        musicPlayer.context.src = 'Eluveitie - Isara.mp3';//musicPlayer.currentMusic.url
    }
    if (musicPlayer.currentMusic.url == null){// url地址不存在则播放下一首
        Vue.prototype.$message.error('地址错误！');
        nextMusic();
        return;
    }
    if (musicPlayer.playState) {
        musicPlayer.context.pause();
    } else {
        musicPlayer.context.play();
    }
    musicPlayer.playState = !musicPlayer.playState
}

/**
 * 替换歌单
 */
export function replaceMusicList(musicList) {
    if (!musicList || musicList.length == 0) {
        return;
    }
    let musicPlayer = Vue.prototype.$bg.musicPlayer
    let len = musicList.length
    let newMusicList = [];
    for (let i = 0; i < len; i++) {
        let record = musicList[i];
        let music = {
            'id': record.id,
            'coverImgUrl': record.al.picUrl,
            'name': record.name,
            'author': record.ar[0].name,
            'duration': record.dt
        }
        newMusicList.push(music)
    }
    // 先清空正在播放音乐列表
    store.commit("SET_MUSIC_CLEAR", []);
    musicPlayer.musicList = [];
    musicPlayer.currentMusic = {};
    // 替换播放列表和当前正在播放音乐
    store.commit('SET_PLAYLIST', newMusicList);

    musicPlayer.musicList = newMusicList;
    musicPlayer.currentMusic = newMusicList[0];
    play(musicPlayer, true)
}


// 上一首
export function previousMusic() {
    let currentMusic = store.getters.currentPlayingMusic;
    let musicList = store.getters.playList;
    let len = musicList.length;
    let index = 0;
    for (let i = 0; i < len; i++) {
        if (currentMusic.id == musicList[i].id) {
            index = i;
            break;
        }
    }
    if (len === 0) {
        return
    }
    if (index == 0) {
        index = len - 1;
    } else {
        index = index - 1;
    }
    let musicPlayer = Vue.prototype.$bg.musicPlayer;
    readyNext(index, musicPlayer, 1)
}

// 下一首
export function nextMusic() {
    let currentMusic = store.getters.currentPlayingMusic;
    let musicList = store.getters.playList;
    let len = musicList.length;
    let index = 0;
    for (let i = 0; i < len; i++) {
        if (currentMusic.id == musicList[i].id) {
            index = i;
            break;
        }
    }
    if (len === 0) {
        return
    }
    if (index !== len - 1) {
        index = index + 1
    } else {
        index = 0;
    }
    let musicPlayer = Vue.prototype.$bg.musicPlayer;
    readyNext(index, musicPlayer, 1)
}
